#!/usr/bin/env bash
set -uo pipefail;

####################################
# Ensure we can execute standalone #
####################################

function early_death() {
  echo "[FATAL] ${0}: ${1}" >&2;
  exit 1;
};

if [ -z "${TFENV_ROOT:-""}" ]; then
  # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
  readlink_f() {
    local target_file="${1}";
    local file_name;

    while [ "${target_file}" != "" ]; do
      cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
      file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
      target_file="$(readlink "${file_name}")";
    done;

    echo "$(pwd -P)/${file_name}";
  };

  TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
  [ -n "${TFENV_ROOT}" ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
else
  TFENV_ROOT="${TFENV_ROOT%/}";
fi;
export TFENV_ROOT;

if [ -n "${TFENV_HELPERS:-""}" ]; then
  log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
else
  [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
  if source "${TFENV_ROOT}/lib/helpers.sh"; then
    log 'debug' 'Helpers sourced successfully';
  else
    early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
  fi;
fi;

# Ensure libexec and bin are in $PATH
for dir in libexec bin; do
  case ":${PATH}:" in
    *:${TFENV_ROOT}/${dir}:*) log 'debug' "\$PATH already contains '${TFENV_ROOT}/${dir}', not adding it again";;
    *)
      log 'debug' "\$PATH does not contain '${TFENV_ROOT}/${dir}', prepending and exporting it now";
      export PATH="${TFENV_ROOT}/${dir}:${PATH}";
      ;;
  esac;
done;

#####################
# Begin Script Body #
#####################

[ "${#}" -gt 1 ] && log 'error' 'usage: tfenv use [<version>]';

declare auto_install="${TFENV_AUTO_INSTALL:-true}";

if [ "${auto_install}" != 'true' ]; then
  [ -d "${TFENV_CONFIG_DIR}/versions" ] \
    || log 'error' 'No versions of terraform installed. Please install one with: tfenv install';
fi;

declare requested_arg="${1:-""}";
declare version_source_suffix="";
declare requested="${requested_arg}";
declare loaded_version_file="$(tfenv-version-file)";

if [ -z "${requested_arg}" -a -z "${TFENV_TERRAFORM_VERSION:-""}" ]; then
  version_source_suffix=" (set by ${loaded_version_file})";

  if [ -f "${loaded_version_file}" ]; then
    requested="$(cat "${loaded_version_file}" || true)";
  else
    # No-one asked for anything, no default version is set either
    requested='latest';
  fi;

elif [ -z "${requested_arg}" ]; then
  version_source_suffix=' (set by TFENV_TERRAFORM_VERSION)';
  requested="${TFENV_TERRAFORM_VERSION}";
fi;

log debug "Resolving version with: tfenv-resolve-version ${requested_arg}";
declare resolved="$(tfenv-resolve-version ${requested_arg})";
[ -z "${resolved}" ] && log 'error' "Failure to resolve version from ${requested_arg}";

log debug "Resolved to: ${resolved}";

declare version="${resolved%%\:*}";
declare regex="${resolved##*\:}";
declare installed_version='';

log 'debug' "Searching ${TFENV_CONFIG_DIR}/versions/ for latest version matching ${regex}";

if [ -d "${TFENV_CONFIG_DIR}/versions" ]; then
  installed_version="$(\find "${TFENV_CONFIG_DIR}/versions/" -type d -exec basename {} \; \
    | tail -n +2 \
    | grep -e "${regex}" \
    | sort -t'.' -k 1nr,1 -k 2nr,2 -k 3r,3 \
    | head -n 1
  )";
fi;

if [ -n "${installed_version}" ]; then
  log 'debug' "Found version: ${installed_version}";
else
  if [ "${auto_install}" == 'true' ]; then
    log 'info' "No installed versions of terraform matched '${resolved}'. Trying to install a matching version since TFENV_AUTO_INSTALL=true";

    declare install_version='';
    [ "${version}" == 'latest' ] && install_version="${version}:${regex}" || install_version="${version}";

    tfenv-install "${install_version}" || log 'error' 'Installing a matching version failed';

    installed_version="$(\find "${TFENV_CONFIG_DIR}/versions/" -type d -exec basename {} \; \
      | tail -n +2 \
      | sort -t'.' -k 1nr,1 -k 2nr,2 -k 3nr,3 \
      | grep -e "${regex}" \
      | head -n 1
    )";

    [ -n "${installed_version}" ] \
      || log 'error' "Despite successfully installing a version matching '${resolved}', a matching version could not be found in '${TFENV_CONFIG_DIR}/versions/' - This should be pretty much impossible";
  fi;
fi;

target_path="${TFENV_CONFIG_DIR}/versions/${installed_version}";
log 'debug' "target_path is ${TFENV_CONFIG_DIR}/versions/${installed_version}";

[ -d "${target_path}" ] \
  || log 'error' "Version directory for ${installed_version} (${target_path}) is not present! Manual intervention required.";
[ -f "${target_path}/terraform" ] \
  || log 'error' "Version directory for ${installed_version} is present, but the terraform binary is not! Manual intervention required.";
[ -x "${target_path}/terraform" ] \
  || log 'error' "Version directory for ${installed_version} is present, but the terraform binary is not executable! Manual intervention required.";

log 'info' "Switching default version to v${installed_version}";
version_file="${TFENV_CONFIG_DIR}/version";
log 'debug' "Writing \"${installed_version}\" to \"${version_file}\"";
echo "${installed_version}" > "${version_file}" \
  || log 'error' "Switch to v${installed_version} failed";

if [ "${version_file}" != "${loaded_version_file}" ]; then
  log 'warn' "Default version file overridden by ${loaded_version_file}, changing the default version has no effect";
fi;

log 'info' "Default version (when not overridden by .terraform-version or TFENV_TERRAFORM_VERSION) is now: ${installed_version}";
